//
//  UKWaterFlowLayout.m
//  HDYS
//
//  Created by qianfeng on 15-5-5.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "UKWaterFlowLayout.h"

@implementation UKWaterFlowLayout
{
    //列数
    NSInteger _colNum;
    //保存高度的数组
    NSMutableArray *_columnArray;
    
    NSMutableArray *_attrArray;
    
}

- (instancetype)init {
    if (self = [super init]) {
        _colNum = 2;
        _columnArray = [NSMutableArray arrayWithCapacity:0];
        _attrArray = [NSMutableArray arrayWithCapacity:0];
    }
    return self;
}

//重新布局就回调用
- (void)prepareLayout {
    [super prepareLayout];
    
    if (self.delegate) {
        _colNum = [self.delegate numberOfColumns];
    }
    
    
    UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
    attr.frame = CGRectMake(0, 0, 375, 90);
    
    [_attrArray addObject:attr];
    
    //起始高度
    for (int i = 0; i < _colNum; i++) {
        NSNumber *n = [NSNumber numberWithFloat:self.sectionInsets.top+90];
        [_columnArray addObject:n];
    }
    
    NSInteger cellNum = [self.collectionView numberOfItemsInSection:0];
    // width
    CGFloat allCellW = self.collectionView.bounds.size.width - self.sectionInsets.left - self.sectionInsets.right - self.colSpace * (_colNum - 1);
    CGFloat width = allCellW / _colNum;
    
    //为所有item保存属性
    for (int i = 0; i < cellNum; i++) {
        // x
        NSInteger curCol = [self currentLowestItemIndex];
        CGFloat x = self.sectionInsets.left + curCol * (width + self.colSpace);
        // y
        CGFloat y = [_columnArray[curCol] floatValue];
        // height
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        CGFloat height = [self.delegate heightforItemAtIndexpath:indexPath];
        
        CGRect frame = CGRectMake(x, y, width, height);
        
        UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        attr.frame = frame;
        
        [_attrArray addObject:attr];
        
        _columnArray[curCol] = [NSNumber numberWithFloat:y + height + self.lineSpace];
    }
    
    
    
}

//- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
//    
//}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    return _attrArray;
}

- (CGSize)collectionViewContentSize {
    NSInteger index = [self currentHeightestItemIndex];
    CGFloat h = [_columnArray[index] floatValue];
    return CGSizeMake(self.collectionView.bounds.size.width, h);
}

//返回最低
- (NSInteger)currentLowestItemIndex {
    NSInteger index = -1;
    CGFloat height = CGFLOAT_MAX;
    
    for (int i = 0; i < _columnArray.count; i++) {
        if ([_columnArray[i] floatValue] < height) {
            height = [_columnArray[i] floatValue];
            index = i;
        }
    }
    return index;
}



//返回最高的
- (NSInteger)currentHeightestItemIndex {
    NSInteger index = -1;
    CGFloat height = CGFLOAT_MIN;
    
    for (int i = 0; i < _columnArray.count; i++) {
        if ([_columnArray[i] floatValue] > height) {
            height = [_columnArray[i] floatValue];
            index = i;
        }
    }
    return index;
}

@end
